package com.haha.helloworld.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.haha.helloworld.R;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class CustomDialog extends Dialog implements View.OnClickListener {
    private TextView tv_title;
    private TextView tv_message;
    private TextView tv_cancel;
    private TextView tv_comfirm;

    private String title,message,cancel,comfirm;

    private IOnCancelListener iOnCancelListener;

    private IOnComfirmListener iOnComfirmListener;

    public CustomDialog(@NonNull Context context) {
        super(context);
    }

    public CustomDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected CustomDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_custom_dialog);
        tv_title= (TextView)findViewById(R.id.tv_title);
        tv_message= (TextView)findViewById(R.id.tv_message);
        tv_cancel= (TextView)findViewById(R.id.tv_cancel);
        tv_comfirm= (TextView)findViewById(R.id.tv_comfirm);
        //设置值
        if (!TextUtils.isEmpty(title)){
            tv_title.setText(title);
        }
        if (!TextUtils.isEmpty(message)){
            tv_message.setText(message);
        }
        if (!TextUtils.isEmpty(cancel)){
            tv_cancel.setText(cancel);
        }
        if (!TextUtils.isEmpty(comfirm)){
            tv_comfirm.setText(comfirm);
        }
        //设置监听事件
        tv_cancel.setOnClickListener(this);
        tv_comfirm.setOnClickListener(this);

    }
    //四个属性的set方法,
    public CustomDialog setTitle(String title) {
        this.title = title;
        //返回的this，就是调用此setTitle方法的类，即是CustomDialog
        return this;
    }

    public CustomDialog setMessage(String message) {
        this.message = message;
        return this;
    }

    public CustomDialog setCancel(String cancel,IOnCancelListener iOnCancelListener) {
        this.cancel = cancel;
        this.iOnCancelListener=iOnCancelListener;
        return this;
    }

    public CustomDialog setComfirm(String comfirm,IOnComfirmListener iOnComfirmListener) {
        this.comfirm = comfirm;
        this.iOnComfirmListener=iOnComfirmListener;
        return this;
    }
    //实现onclickListener接口的方法
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_cancel:
                if (iOnCancelListener!=null){
                    iOnCancelListener.onCancel(this);
                }
                dismiss();
                break;
            case R.id.tv_comfirm:
                if (iOnComfirmListener!=null){
                    iOnComfirmListener.onComfirm(this);
                }
                dismiss();
                break;
        }

    }

    //回调接口
    public interface IOnCancelListener{
        void onCancel(CustomDialog dialog);
    }

    public interface IOnComfirmListener{
        void onComfirm(CustomDialog dialog);
    }

}
